﻿#region base
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using hachiModel;

[WebService(Namespace = "http://hachihachi.com.vn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
[System.Web.Script.Services.ScriptService]
#endregion
public class WebService : System.Web.Services.WebService
{
    #region innit
    Hachi.Controllers.CategoriesController catectr = new Hachi.Controllers.CategoriesController();
    Hachi.Controllers.ProductsController productctr = new Hachi.Controllers.ProductsController();
    Hachi.Controllers.SlideShowController slctr = new Hachi.Controllers.SlideShowController();
    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    #endregion

    #region CategoryGetCanUse
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCategoryCanUse()
    {
        List<hachiModel.PR_Category> cates = catectr.GetCategoriesCanUse();
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(cates.Select(s => new {s.ParentID, s.CategoryID, s.CategoryCode }));
        return strJSON;
    }
    #endregion

    #region CategoryGetCanUse_list
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public List<hachiModel.PR_Category> GetCategoryCanUse_list()
    {
        List<hachiModel.PR_Category> cates = catectr.GetCategoriesCanUse();
        return cates;
    }
    #endregion

    #region PromotionProductsGetTop12
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetTop12PromotionProducts(string Lang)
    {
        List<PR_Product> pl = productctr.GetTop12PromotionProducts(Lang);
        return ProductJson(pl, Lang);
    }
    #endregion

    #region ProductsNewGetTop12
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetTop12NewProducts(string Lang)
    {
        List<PR_Product> pl = productctr.getListProductInType(5).Take(12).ToList();
        return ProductJson(pl, Lang);
    }
    #endregion

    #region ProductsNewGet
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetNewProducts(string Lang, int Take, int Skip, decimal FromPrice, decimal ToPrice)
    {
        List<PR_Product> pl = productctr.getListProductInType(5).Where(d => d.Price >= FromPrice && d.Price <= ToPrice).Skip(Skip).Take(Take).ToList();
        return ProductJson(pl, Lang);
    }
    #endregion

    #region ProductsNewCount
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string CountNewProduct(string Lang, decimal FromPrice, decimal ToPrice)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(productctr.getListProductInType(5).Where(d => d.Price >= FromPrice && d.Price <= ToPrice).Count());
        return strJSON;

    }
    #endregion

    #region ProductsGetByCategoryID
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetProductsByCategoryID(int CategoryID, string Lang, int Take, int Skip, decimal FromPrice, decimal ToPrice)
    {
        List<PR_Product> pl = productctr.GetProductsByCategoryID(Lang, CategoryID, Take, Skip, FromPrice, ToPrice);
        return ProductJson(pl, Lang);
    }
    #endregion

    #region ProductCountByCategoryID
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string CountProductByCategoryID(int CategoryID, string Lang, decimal FromPrice, decimal ToPrice)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(catectr.CountProduct(CategoryID,Lang, FromPrice, ToPrice));
        return strJSON;
        
    }
    #endregion

    #region SlideShowGet
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetSlideShow(int SlideShowCategoryID, string Lang)
    {
        List<CF_SlideShowDetail> sl = slctr.getSlideShow(SlideShowCategoryID,Lang);
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(sl.Select(s =>
            new
            {
                s.Title,
                s.Image,
                s.Link
            }));
        return strJSON;
    }
    #endregion

    #region ProductCommentGet
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetProductComment(int pProductID)
    {
        List<PR_ProductComment> cl = productctr.GetProductComment(pProductID);
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(cl.Select(s =>
            new
            {
                s.UserName,
                s.Comment,
                s.Vote,
                s.CommentID,
                ModifiedDate = s.ModifiedDate.ToString("HH:mm - dd/MM/yyyy")
            }));
        return strJSON;
    }
    #endregion


    #region PromotionsCount
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string PromotionsCount()
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(productctr.CountPromotions());
        return strJSON;
    }
    #endregion

    #region PromotionsGet
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string PromotionsGet(string Lang, int Take, int Skip)
    {
        List<CS_Promotion> pl = productctr.GetPromotions(Take, Skip);
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(pl.Select(s =>
            new
            {
                s.PromotionID,
                s.PromotionCode,
                productctr.GetPromotionDetail(s,Lang).PromotionName,
                productctr.GetPromotionDetail(s, Lang).PromotionSummary,
                StartDate = s.StartDate.ToString("HH\\h dd/MM/yyyy"),
                EndDate = s.EndDate.ToString("HH\\h dd/MM/yyyy")
            }));
        return strJSON;
    }
    #endregion

    #region PromotionGetProducts
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string PromotionGetProducts(int pPromotionID, string Lang, int Take, int Skip, decimal FromPrice, decimal ToPrice)
    {
        List<PR_Product> pl = productctr.GetPromotionProducts(Lang, pPromotionID, Take, Skip, FromPrice, ToPrice);
        return ProductJson(pl, Lang);
    }
    #endregion

    #region PromotionProductsCount
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string PromotionProductsCount(int pPromotionID, string Lang, decimal FromPrice, decimal ToPrice)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(productctr.PromotionProductsCount(Lang, pPromotionID, FromPrice, ToPrice));
        return strJSON;
    }
    #endregion
    
    #region ProductTypeGetProducts
    /*
    1	Trang chủ
    2	Sản phẩm độc đáo
    3	Sản phẩm khuyên dùng
    4	
    5	Hàng mới về
     */
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string ProductTypeGetProducts(int ProductTypeID,  string Lang, int Take, int Skip, decimal FromPrice, decimal ToPrice)
    {
        List<PR_Product> pl = productctr.ProductTypeGetProducts(ProductTypeID,Take,Skip,FromPrice,ToPrice);
        return ProductJson(pl,Lang);
    }
    #endregion

    #region ProductTypeProductsCount
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string ProductTypeProductsCount(int ProductTypeID, decimal FromPrice, decimal ToPrice)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(productctr.ProductInTypeProductsCount(ProductTypeID,FromPrice,ToPrice));
        return strJSON;
    }
    #endregion

    



    #region SearchGetProducts
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SearchGetProducts(string Key, string Lang, int Take, int Skip, decimal FromPrice, decimal ToPrice)
    {
        List<PR_Product> pl = productctr.SearchGetProducts(Key, Take, Skip, FromPrice, ToPrice);
        return ProductJson(pl, Lang);
    }
    #endregion

    #region SearchProductsCount
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SearchProductsCount(string Key, decimal FromPrice, decimal ToPrice)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(productctr.SearchProductsCount(Key, FromPrice, ToPrice));
        return strJSON;
    }
    #endregion

    private string ProductJson(List<PR_Product> pl, string Lang)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(pl.Select(s =>
            new
            {
                s.ProductID,
                s.DisplayCode,
                s.Quantity,
                Price = String.Format("{0:#,#.# vnđ}", s.Price),
                IsSaleOff = productctr.IsPromotionProduct(s.ProductID),
                IsNew = productctr.IsInType(s.ProductID,5),
                PriceSaleOff = String.Format("{0:#,#.# vnđ}", s.PriceSaleOff),
                s.Hit,
                productctr.getProductDetailToShow(s, Lang).ProductName,
                productctr.getProductDetailToShow(s, Lang).Alias,
                IsRecommended = productctr.IsInType(s.ProductID,3),
                IsUnique = productctr.IsInType(s.ProductID, 2),
                IsTopical = productctr.IsTopicalProduct(s.ProductID)
            }));
        return strJSON;
    }
}
